package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.Main;
import gui.Delivery_Greeting_Gui;
import gui.Payment_GUI;
import gui.Order_GUI;
import client.CreditCard;

public class CreditCard_GUI extends JFrame implements ActionListener
{
	//variables
	int selected=0;
	String[] month={"1","2","3","4","5","6","7","8","9","10","11","12"};
	String[] year=new String[5];
	int newcredit=0;
	public CreditCard cc=null;
	public CreditCard cc1=null;
	String num=null;
	String expM=null;
	String expY=null;
	String numbers[]=null;
	int noOldCredit=-1;
	
	//panels
	JPanel panelNewCredit=null;
	JPanel panelPrimaryCredit=null;
	
	//combo box
	JComboBox cbExpireDateMonth;
	JComboBox cbExpireDateYear;
	JComboBox cbCreditnumbers;
	JComboBox cbExpireDate;
	
	//buttons
	public JButton Back=null;
	JButton btnCancel=null;//back from the newCreditPanel
	JButton newCredit=null;
	JButton btnSubmit=null;
	JButton btnApprove=null;
	
	//labels
	JLabel title=null;
	JLabel lblCustomerCreditCardNumer=null;
	JLabel lblExpireDate=null;
	JLabel lblCreditCardNumber=null;
	
	//TextField
	JTextField txtCustomerCreditCardNumber=null;;
	JTextField txtExpireDate=null;;
	
	public void hideCreditScreen()
	{
		this.setEnabled(false);
		this.setVisible(false);
	}
	
	public void showCreditScreen()
	{
		this.setEnabled(true);
		this.setVisible(true);
	}
	void init()
	{
		this.setSize(410,410);
		this.setTitle("Order Payment");
		cc1=new CreditCard();
		numbers=cc1.getUpdatedCreditCardNumbers();
		if(numbers.length==0||numbers.equals(""))
		{
			noOldCredit=1;
			this.add(getPanelNewCredit());
		}
		else 
		{
			this.add(getPanelPrimary());
		}
		this.setLocation((int)Main.dim.getWidth()/2 - (int)this.getPreferredSize().getWidth()/2,(int)Main.dim.getHeight()/4 - (int)this.getPreferredSize().getHeight()/4);
		this.setResizable(false);	
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);	
		showCreditScreen();
	}
	
	JPanel getPanelPrimary()
	{
		panelPrimaryCredit= new JPanel();
		panelPrimaryCredit.setLayout(null);
		panelPrimaryCredit.add(getLabelTitel("Credit Card"));
		panelPrimaryCredit.add(getButtonBack());
		panelPrimaryCredit.add(getButtonApprove());
		panelPrimaryCredit.add(getButtonNewCredit());
		panelPrimaryCredit.add(getLabelCreditCardNumer());
		panelPrimaryCredit.add(getComboCreditnumbers());
		panelPrimaryCredit.add(getComboExpireDate());
		panelPrimaryCredit.add(getLabelExpireDate());
		panelPrimaryCredit.add(getLabelCustomerCreditCardNumer());
		panelPrimaryCredit.add(getTextCustomerCreditCard());
		panelPrimaryCredit.setVisible(true);
		panelPrimaryCredit.setEnabled(true);
		return panelPrimaryCredit;
	}
	
	JPanel getPanelNewCredit()
	{
		panelNewCredit= new JPanel();
		panelNewCredit.setLayout(null);
		panelNewCredit.add(getLabelTitel("New Credit Card"));
		if(noOldCredit==1)	panelNewCredit.add(getButtonBack());
		else  panelNewCredit.add(getButtonCancel());
		panelNewCredit.add(getComboExpireDateMonth());
		panelNewCredit.add(getComboExpireDateYear());
		panelNewCredit.add(getLabelExpireDate());
		panelNewCredit.add(getButtonSubmit());
		panelNewCredit.add(getLabelCustomerCreditCardNumer());
		panelNewCredit.add(getTextCustomerCreditCard());
		panelNewCredit.setVisible(true);
		panelNewCredit.setEnabled(true);
		return panelNewCredit;
	}
	JLabel getLabelTitel(String str)
	{
		title=new JLabel();
		title.setText(str);
		title.setBounds(110, 50, 300, 40);
		title.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		return title;
	}
	JButton getButtonBack()//back of the main panel
	{
		if(Back==null)
		{
			Back=new JButton();
			Back.setText("Back");
			Back.setBounds(290,10,100,40);
		}
		return Back;
	}
	JButton getButtonCancel()//back of the main panel
	{
		if(btnCancel==null)
		{
			btnCancel=new JButton();
			btnCancel.setText("Cancel");
			btnCancel.setBounds(290,10,100,40);
			btnCancel.addActionListener(this);
		}
		return btnCancel;
	}
	JButton getButtonNewCredit()
	{
		if(newCredit==null)
		{
			newCredit=new JButton();
			newCredit.setText(" + Add New Credit Card");
			newCredit.setBounds(200, 290, 170, 40);
			newCredit.addActionListener(this);
		}
		return newCredit;
	}
	JButton getButtonApprove()
	{
		if(btnApprove==null)
		{
			btnApprove=new JButton();
			btnApprove.setText("Approve");
			btnApprove.setBounds(90, 290, 100, 40);
			btnApprove.addActionListener(this);
		}
		return btnApprove;
	}
	JButton getButtonSubmit()
	{
			btnSubmit=new JButton();
			btnSubmit.setText("Submit");
			btnSubmit.setBounds(200, 290, 170, 40);
			btnSubmit.addActionListener(this);
		return btnSubmit;
	}
	
	JComboBox getComboCreditnumbers()
	{
		cbCreditnumbers=new JComboBox();
		cbCreditnumbers.setBounds(200, 120, 120, 20);
		for(int i=0;i<numbers.length;i++)cbCreditnumbers.addItem(numbers[i]);
		cbCreditnumbers.addActionListener(this);
		return cbCreditnumbers;
	}
	JComboBox getComboExpireDateMonth()
	{
			cbExpireDateMonth=new JComboBox();
			cbExpireDateMonth.setBounds(200, 200, 50, 20);
			if(newcredit==1)
				for(int i=0;i<month.length;i++)	cbExpireDateMonth.addItem(month[i]);
		return cbExpireDateMonth;
	}
	
	JComboBox getComboExpireDateYear()
	{
			cbExpireDateYear=new JComboBox();
			cbExpireDateYear.setBounds(270, 200, 70, 20);
			year=cc1.getYears();
			if(newcredit==1)
				for(int i=0;i<year.length;i++)	cbExpireDateYear.addItem(year[i]);
		return cbExpireDateYear;
	}
	JComboBox getComboExpireDate()
	{
			cbExpireDate=new JComboBox();
			cbExpireDate.setBounds(270, 200, 70, 20);
			cbExpireDate.addItem(cc1.getexpirationDate());
			cbExpireDate.setEnabled(false);
		return cbExpireDate;
	}
	
	JLabel getLabelExpireDate()
	{
		if(lblExpireDate==null)
		{
			lblExpireDate=new JLabel();
			lblExpireDate.setText("Expire Date:");
			lblExpireDate.setBounds(40, 200, 200, 20);
			lblExpireDate.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblExpireDate;
	}
	
	JLabel getLabelCreditCardNumer()
	{
		if(lblCreditCardNumber==null)
		{
			lblCreditCardNumber=new JLabel();
			lblCreditCardNumber.setText("Select Credit Card:");
			lblCreditCardNumber.setBounds(20, 120, 180, 20);
			lblCreditCardNumber.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblCreditCardNumber;
	}
	
	JLabel getLabelCustomerCreditCardNumer()
	{
		if(lblCustomerCreditCardNumer==null)
		{
			lblCustomerCreditCardNumer=new JLabel();
			lblCustomerCreditCardNumer.setText("Credit Card Number:");
			lblCustomerCreditCardNumer.setBounds(20, 160, 180, 20);
			lblCustomerCreditCardNumer.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblCustomerCreditCardNumer;
	}
	
	JTextField getTextCustomerCreditCard()
	{
			txtCustomerCreditCardNumber=new JTextField();
			txtCustomerCreditCardNumber.setBounds(190, 160, 200, 20);
			txtCustomerCreditCardNumber.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
			if((newcredit==1)||(noOldCredit==1))	txtCustomerCreditCardNumber.setText("");
			else{txtCustomerCreditCardNumber.setText(cc1.getcardNumber());
			txtCustomerCreditCardNumber.setEnabled(false);}
		return txtCustomerCreditCardNumber;
	}

	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource()==newCredit)	goNewCard();
		if(e.getSource()==btnCancel)//cancel new card back to old card	
		{
			int choice;//0-yes 1-no
			choice=JOptionPane.showConfirmDialog(null,"Are U sure you want to cancel ? new card hasn't submited!","Credit Card",JOptionPane.YES_NO_OPTION);
			if(choice==0)	
			{
				if(noOldCredit==1)
				{
					hideCreditScreen();
					Main.main.showMainScreen();
				}
				else goOldCard();
			}
		}
		if(e.getSource()==cbCreditnumbers)
		{
			cc1.setcardNumber((String) cbCreditnumbers.getSelectedItem());
			cc1.setexpireMonth("-1");
			cc1.setexpireYear("-1");
			cc1=cc1.getSelectedCreditCard();
			if(cc1==null)JOptionPane.showMessageDialog(null,"Credit Card number not found in db\n please choose new one  ","Credit Card - ERROR",JOptionPane.ERROR_MESSAGE);
			else {
				panelPrimaryCredit.setVisible(false);;
				this.add(getPanelPrimary());
			}
		}
		if(e.getSource()==btnApprove)approvePayment();//old card
		if(e.getSource()==btnSubmit)//new card
		{
			num=txtCustomerCreditCardNumber.getText();
			expM=(String) cbExpireDateMonth.getSelectedItem();
			expY=(String) cbExpireDateYear.getSelectedItem();
			cc=new CreditCard(num,  expM, expY);
			int flag=cc.validateCreditCardNumer();
			if(flag==0)JOptionPane.showMessageDialog(null,"Credit Card number not valid\nPlease type 16 digits ","Credit Card - ERROR",JOptionPane.ERROR_MESSAGE);	
			int flagd=cc.validateExpireDate();
			if(flagd==0)    JOptionPane.showMessageDialog(null,"Expaier Date isn't correct\nPlease try again!","Credit Card - ERROR",JOptionPane.ERROR_MESSAGE);
			if((flag==1)&(flagd==1))	
			{
				int i=cc.submitNew();
				if(i==0)JOptionPane.showMessageDialog(null,"Fail to save new credit card - try again!","Credit Card - ERROR",JOptionPane.ERROR_MESSAGE);
				else
				{ 
					if(i==-1)	JOptionPane.showMessageDialog(null,"The credit card number allready exist in DB","Credit Card - ERROR",JOptionPane.INFORMATION_MESSAGE);
					approvePayment();
				}
			}
		}
	}
	
	void approvePayment()
	{
		 JOptionPane.showMessageDialog(null,"Your Credit Card will be Charge\n with the total order price: "+Main.cart.total,"Credit Card - Approve Payment",JOptionPane.INFORMATION_MESSAGE);
		 Main.cart.deliveryGreeting.payment_gui.order_payment="credit";
		 hideCreditScreen();
		 
		 Main.main.orderCallFrom = "CreditCard_GUI";
		 Main.main.Orders.doClick();
	}
	void goNewCard()
	{
		panelPrimaryCredit.setVisible(false);
		panelPrimaryCredit.setEnabled(false);
		newcredit=1;
		selected=0;
		this.add(getPanelNewCredit());
	}
	void goOldCard()
	{
		panelNewCredit.setVisible(false);
		panelNewCredit.setEnabled(false);
		newcredit=0;
		selected=0;
		this.add(getPanelPrimary());
	}
	
	public CreditCard_GUI()//constructor 
	{		
		super();
		init();	
	}
}
